﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace Sispe.Mvc4.Controllers
{
    public class PermisoController : Controller
    {
        [Dependency]
        public IPermisoBL permisoBL { get; set; }

        [Dependency]
        public IContratoBL contratoBL { get; set; }

        [Dependency]
        public ITipoPermisoBL tipoPermisoBL { get; set; }

        public ActionResult Index(int id = 0)
        {
            if (id == 0)
            {
                return RedirectToAction("Index", "Contrato");
            }
            else
            {
                var permisos = permisoBL.GetFromPermisoByIdContrato(id);
                

                var contrato = contratoBL.GetFromContratoById(id);
                if (contrato == null)
                {
                    return RedirectToAction("Index", "Contrato");
                }

                ViewData["IdContrato"] = contrato.IdContrato;
                ViewData["TRABAJADOR"] = contrato.TRABAJADOR.PERSONA.NombreCompleto;

                return View(permisos);
            }
        }


        public ActionResult Create(int id = 0)
        {
            var tipoPermiso = tipoPermisoBL.GetAllFromTipoPermiso();
            ViewData["IdTipoPermiso"] = new SelectList(tipoPermiso, "IdTipoPermiso", "Descripcion");

            ViewData["IdContrato"] = id;
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection colection)
        {
            var a = colection["FechaInicio"].ToString();
            var b = colection["FechaTermino"].ToString();
            var FechaInicio = a.Substring(3, 2) + "-" + a.Substring(0, 2) + "-" + a.Substring(6, 4);
            var FechaTermino = b.Substring(3, 2) + "-" + b.Substring(0, 2) + "-" + b.Substring(6, 4);


            try
            {
                PERMISO permiso = new PERMISO()
                {
                    IdContrato = Int32.Parse(colection["IdContrato"].ToString()),
                    FechaInicio = Convert.ToDateTime(FechaInicio).Date,
                    FechaTermino = Convert.ToDateTime(FechaTermino).Date,
                    IdTipoPermiso = Int32.Parse(colection["IdTipoPermiso"].ToString()),
                    Observacion = colection["Observacion"].ToString(),
                    Dias = colection["Dias"].ToString(),
                    
                };

                permisoBL.AddPermiso(permiso);
                return RedirectToAction("Index", "Permiso", new { @id = permiso.IdContrato });
            }
            catch (Exception ex)
            {
                TempData["AlertMessage"] = ex.Message;
                return RedirectToAction("Create");
            }
        }


        public ActionResult Edit(int id = 0)
        {

            if (id == 0)
            {
                return View();
            }

            var currPermiso = new PERMISO();

            var permiso = (from c in permisoBL.GetAllFromPermiso() where c.IdPermiso.Equals(id)
                           select c).ToList();

            foreach (var item in permiso.Where(c => c.IdPermiso.Equals(id)))
            {
                var fecIniBD = item.FechaInicio.Value.ToShortDateString();
                var fecFinBD = item.FechaTermino.Value.ToShortDateString();

                ViewData["FechaInicio"] = fecIniBD.Substring(3, 2) + "-" + fecIniBD.Substring(0, 2) + "-" + fecIniBD.Substring(6, 4);
                ViewData["FechaTermino"] = fecFinBD.Substring(3, 2) + "-" + fecFinBD.Substring(0, 2) + "-" + fecFinBD.Substring(6, 4);
                               
                
                currPermiso.IdPermiso = item.IdPermiso; 
                currPermiso.IdContrato = item.IdContrato;
                currPermiso.IdTipoPermiso = item.IdTipoPermiso;
                currPermiso.Dias = item.Dias;
                currPermiso.Observacion = item.Observacion;
            }

            if (currPermiso == null)
            {
                return RedirectToAction("Index", "Contrato");
            }

            var tipoPermiso = tipoPermisoBL.GetAllFromTipoPermiso();

            ViewData["IdTipoPermiso"] =
                new SelectList(tipoPermiso, "IdTipoPermiso", "Descripcion", currPermiso.IdTipoPermiso);

            return View(currPermiso);
        }


        [HttpPost]
        public ActionResult Edit(int id, FormCollection colection)
        {
            var a = colection["FechaInicio"].ToString();
            var b = colection["FechaTermino"].ToString();
            var FechaInicio = a.Substring(3, 2) + "-" + a.Substring(0, 2) + "-" + a.Substring(6, 4);
            var FechaTermino = b.Substring(3, 2) + "-" + b.Substring(0, 2) + "-" + b.Substring(6, 4);


            try
            {
                PERMISO permiso = new PERMISO()
                {
                    IdPermiso = id, 
                    IdContrato = Int32.Parse(colection["IdContrato"].ToString()),
                    FechaInicio = Convert.ToDateTime(FechaInicio).Date,
                    FechaTermino = Convert.ToDateTime(FechaTermino).Date,
                    IdTipoPermiso = Int32.Parse(colection["IdTipoPermiso"].ToString()),
                    Observacion = colection["Observacion"].ToString(),
                    Dias = colection["Dias"].ToString(),
                };

                permisoBL.UpdatePermiso(permiso);
                return RedirectToAction("Index", "Permiso", new { @id = permiso.IdContrato });
            }
            catch (Exception ex)
            {
                TempData["AlertMessage"] = ex.Message;
                return View();
            }
        }





    }
}
